#!/usr/bin/env python3

import itchat
import requests


def get_response(msg):
    date = {
            'key': '6cd088d85dc84596bef960d84615bcf8',
            'info': msg,
            'userid': 'you_id_of_tuling'
            }
    url = 'http://www.tuling123.com/openapi/api'
    rep = requests.post(url, date)
    rep.encoding = 'utf-8'
    try:
        reps = rep.json().get('text')
        re_url = rep.json().get('url')
        re_lists = rep.json().get('lists')
        """ 当json中没有'url','lists'时，get方法返回None,"""
        if not re_url and not re_lists:
            return reps
        elif not re_lists:
            return reps+'\n'+re_url
        elif not re_url:
            return reps +'\n'+ re_lists
    except:
        return


@itchat.msg_register(itchat.content.TEXT)
def reply_msg(a):
    d = get_response(a['Text'])
    b = "我收到了" + a['Text']
    return d or b
itchat.auto_login(hotReload=True)
itchat.run()
